package com.ugoodtech.mdcc.core.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.Department;
import com.ugoodtech.mdcc.core.domain.QDepartment;
import com.ugoodtech.mdcc.core.repository.DepartmentRepository;
import com.ugoodtech.mdcc.core.service.DepartmentCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentCoreServiceImpl implements DepartmentCoreService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public List<Long> parentDepartment() {
        QDepartment qDepartment = QDepartment.department;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qDepartment.deleted.eq(false));
        builder.and(qDepartment.enabled.eq(true));
        builder.and(qDepartment.parent.isNull());
        List<Department> departmentList = (List<Department>) departmentRepository.findAll(builder);
        return responseDepartment(departmentList);
    }

    @Override
    public List<Long> getLowerDepartment(Long departmentId) {
        QDepartment qDepartment = QDepartment.department;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qDepartment.deleted.eq(false));
        builder.and(qDepartment.enabled.eq(true));
        builder.and(qDepartment.parent.id.eq(departmentId));
        List<Department> departmentList = (List<Department>) departmentRepository.findAll(builder);
        return responseDepartment(departmentList);
    }

    private List<Long> responseDepartment(List<Department> departmentList) {
        List<Long> parentDepartmentId = new ArrayList<>();
        for (Department department : departmentList) {
            parentDepartmentId.add(department.getId());
        }
        return parentDepartmentId;
    }


}
